package com.news.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sun.media.sound.JavaSoundAudioClip;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

public class JsonUtils {
    /** 
     * 解析json 
     *  
     * @param json
     * @param valueType 
     * @return 
     */  
    public static <T> T fromJson(String json, Class<T> valueType) {
        return JSON.parseObject(json,valueType);
    }  
  
    /** 
     * 生成json 
     *  
     * @param object 
     * @return 
     */  
    public static String toJson(Object object) {
        try {
            return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }

    public static String getJson(HttpServletRequest request) {
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = request.getReader();
            String line = reader.readLine();
            while (line != null) {
                builder.append(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

}  
